package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.activity.CommentActivity.MyAdapter;
import com.ycsj.goldmedalnewconcept.bean.ClassQueryResponse;
import com.ycsj.goldmedalnewconcept.bean.ClassQueryResponse.ClassQueryInfo;
import com.ycsj.goldmedalnewconcept.bean.SumCommentResp;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

public class ClassQueryActivity extends Activity {

	private GridView gridview_classquery;
	private ImageView iv_back;
	private TextView tv_nodata;
	private int page = 0;
	private int total;
	Handler handler = new Handler();
	public List<ClassQueryInfo> unit = new ArrayList<ClassQueryInfo>();
	private String account;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.fragment_classquery);
		account = SPUtil.getString(this, "account", "");
		initView();
		setOnListener();
		initData();

	}

	private void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_nodata = (TextView) findViewById(R.id.tv_nodata);
		gridview_classquery = (GridView) findViewById(R.id.gridview_classquery);
	}

	private void setOnListener() {

		iv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initData() {
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// 创建一个Request
//		Request request = new Request.Builder().url(
//				Constant.SERVER_URL+"ManageMsg"+"?type=43&c="
//						+ SPUtil.getString(this, "account", "")+"&role=1&page="+page).build();
		Request request = new Request.Builder().url(
				Constant.SERVER_URL+"ManageMsg"+"?type=43&c="
						+ account
						+"&role=1&page="+page).build();
		// http://pm.moregolden.com/ycsj_platform/ManageMsg?type=43&c=18572668972&role=1&page=0 
		Call call = mOkHttpClient.newCall(request);
		call.enqueue(callBack);
	}

	

	
	Callback callBack = new Callback() {

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json = arg0.body().string();
			Log.e("testtt", json);
			Gson gson = new Gson();
			ClassQueryResponse fromJson = gson.fromJson(json,
					ClassQueryResponse.class);
			
			if("-1".equals(fromJson.state)){
				
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {

						tv_nodata.setVisibility(tv_nodata.VISIBLE);
					}
				}, 0);
				
			}else{
			
			if(fromJson!=null&&fromJson.list.size()!=0){
				
				total = Integer.parseInt(fromJson.total);
				unit = fromJson.list;
		
			if ("1".equals(fromJson.state)) {
				
				handler.postDelayed(new Runnable() {

					@Override
					public void run() {

						gridview_classquery.setAdapter(new MyAdapter());
					}
				}, 0);
			}
			}
			}
		}

		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}
	};
	
	class MyAdapter extends BaseAdapter {
		private LayoutInflater inflater = LayoutInflater
				.from(ClassQueryActivity.this);

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return unit.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder viewHolder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.gv_item_classquery,
						null);
				viewHolder = new ViewHolder();
				viewHolder.tv_classunit = (TextView) convertView
						.findViewById(R.id.tv_classunit);
				viewHolder.tv_time = (TextView) convertView
						.findViewById(R.id.tv_time);
				viewHolder.rl_bg = (RelativeLayout) convertView
						.findViewById(R.id.rl_bg);

				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			
			 viewHolder.tv_classunit.setText("第"+(total-position)+"次课");
			 viewHolder.tv_time.setText(unit.get(position).attendance_date);
			 
			 
			 if("0".equals(unit.get(position).attendance_type)||"1".equals(unit.get(position).attendance_type)){
				 viewHolder.rl_bg.setBackgroundResource(R.drawable.classtime_bg);
			 }else{
				 viewHolder.rl_bg.setBackgroundResource(R.drawable.classmiss_bg);
			 }
			 
			// if(unit!=null){
			// for (int i = 0; i < unit.size(); i++) {
			// if ((position+1)==unit.get(i)) {
			// viewHolder.tv_classunit.setBackgroundColor(0xfffd8023);
			// }
			// }
			// }
			return convertView;
		}

		class ViewHolder {
			public TextView tv_classunit;
			public TextView tv_time;
			public RelativeLayout rl_bg;
		}
	}
}
